<?php
/**
 * 装修模块->单页面
 */

namespace Custom\Controller;
use Think\Controller;

class DesignController extends UserController
{
    function __construct()
    {
        parent::__construct();
        $this->M_Sum = M('Sum_view');
        $this->M_View = M('View_name');
    }

    /**
     * 显示修改页
     */
    public function index(){
        if( IS_GET ) {
            $view_id = I('get.id',1,'intval');
            $info = $this->M_Sum->where('c_id = '.$this->c_id.' AND view_pid = '.$view_id)->find();
            if( $info ){
                $data = $this->M_View->field('view_name_html')->where(' view_id = '.$info['view_id'])->find();
                $data = explode('/',$data['view_name_html']);
                if($data){
                    $view_title = $this->M_View->where("view_pid = 0")->select();
                    $this->view_title = $view_title;
                    $view_name = $this->M_View->where("view_pid = ".$view_id)->select();
                    $this->view_name = $view_name;
                    $this->view_id = $view_id;
                    $this->info_option = unserialize($info['sum_option']);
                    $this->sum_option = json_encode(unserialize($info['sum_option']));
                    $this->first = $this->fetch($data[0]);
                    $this->first_detail = $this->fetch($data[1]);
                    $this->display();die;
                }
            }
            $view_title = $this->M_View->where("view_pid = 0")->select();
            $this->view_title = $view_title;
            $view_name = $this->M_View->where("view_pid = ".$view_id)->select();
            $this->view_name = $view_name;
            $this->view_id = $view_id;
            $this->display();
        }
    }

    /**
     * 通过ajax接入到页面
     */
    public function display_view(){
        if(IS_AJAX){
            $code = I('get.id',0,'intval');
            $view_info = $this->M_View->field('view_name_html')->where("view_id = ".$code )->find();
            $view_info = explode('/',$view_info['view_name_html']);
            if( $view_info){
                $this->ajaxReturn(array('id'=>$code,'info'=>$this->fetch($view_info[0]),'detail'=>$this->fetch($view_info[1])));
            }
        }
    }

    /**
     * 保存属性到数据库
     */
    public function save(){
        if(IS_AJAX){
            $data = I('post.');
            $config = array(
                'maxSize' => 8388608,//上传文件的最大值
                'rootPath' => './Uploads/',//上传文件的保存根目录
                'savePath' => '/Design/',//上传文件的上一级目录
                'exts' => array('jpg', 'gif', 'png', 'jpeg'),//允许上传文件格式
            );
            $Upload = new \Think\Upload($config);//实例化上传类
            $images = $Upload->upload();//调用上传类的上传方法
            foreach($images as $key=>$value){
                if(file_exists('./Uploads/'.$data[$key])){
                    unlink('./Uploads/'.$data[$key]);
                }
                $data[$key] = $value['savepath'].$value['savename'];
            }
            $info['sum_option'] = serialize($data);
            $info['add_time'] = time();
            $info['c_id']  = $this->c_id;
            $info['sum_name'] = $data['title'];
            $info['view_id'] = $data['id'];
            $info['view_pid'] = $data['pid'];
            $a = $this->M_Sum->where('view_pid = '.$data['pid'])->find();
            if($a){
                $res = $this->M_Sum->where('view_pid = '.$data['pid'])->save($info);
            }else{
                $res = $this->M_Sum->add($info);
            }
            if ($res){
                $this->ajaxReturn(array('code'=>1,'messages'=>L('chenggongbaocun')));
            }else{
                $this->ajaxReturn(array('code'=>0,'messages'=>L('baocunshibai')));
            }
        }
    }
}